/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CommonProfile;


import CRUD.AdminFacadeLocal;
import CRUD.BranchEnterpriseFacadeLocal;
import CRUD.EnterpriseFacadeLocal;
import CRUD.LocationFacadeLocal;
import CRUD.PersonalInfoFacadeLocal;
import Entity.Admin;
import Entity.AdminPK;
import Entity.BranchEnterprise;
import Entity.BranchEnterprisePK;
import Entity.Enterprise;
import Entity.Location;
import Entity.PersonalInfo;
import Entity.PersonalInfoPK;
import java.math.BigInteger;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author WinKiQue58
 */
@Stateless
public class Sign_Up implements Sign_UpLocal {

   
     @PersistenceContext(unitName = "S3R4_Orcl-ejbPU")
            private EntityManager em;
    
    
    @EJB  private BranchEnterpriseFacadeLocal branchEnterpriseFacade;
    @EJB  private EnterpriseFacadeLocal enterpriseFacade;
    @EJB  private PersonalInfoFacadeLocal personalInfo;
    @EJB  private AdminFacadeLocal adminFacade;
    @EJB  private LocationFacadeLocal locationFacade;
    
    
   
    
    public boolean createSignUp ( Integer userType, String name,  String realName, String pass, String email,
                                  String cellphone, String address, Integer phone, boolean multilevel,
                                  String enterpriseName, String Latitude, String Longitude )
    {
        BigInteger addNumber= new BigInteger("1");
        
        try{
        //--First is neccesary to create Enterprise.   
        Enterprise e;
        Query q1= em.createQuery("SELECT MAX(e.idEnterprise) FROM Enterprise e");
        BigInteger max=(BigInteger)q1.getSingleResult();
        e= new Enterprise ( (max.add(addNumber)), name );
        enterpriseFacade.create(e);
         
        //--Secondly we Create a Branch Enterprise.
        Location l;
        Query q2= em.createQuery("SELECT MAX(l.idLocation) FROM Location l");
        BigInteger max2=(BigInteger)q2.getSingleResult();
        l=new Location(max2.add(addNumber), Latitude, Longitude, address,"Bogota" ,"Colombia");
        locationFacade.create(l);
        
        BranchEnterprise be;
        BranchEnterprisePK bePK= new BranchEnterprisePK(addNumber, (max.add(addNumber)));
        if (multilevel)  be= new BranchEnterprise(bePK,1,l);
        else  be= new BranchEnterprise(bePK,0,l );
        branchEnterpriseFacade.create(be);
        
        //--Creating Personal Info.
        PersonalInfo p;
        PersonalInfoPK pPK=new PersonalInfoPK(addNumber, addNumber, (max.add(addNumber)));
        p=new PersonalInfo(pPK, addNumber, 2, realName, realName, email, 
                (new BigInteger(cellphone)), name, pass,l);
        personalInfo.create(p);
       
        //--Creating the Administrator
        Admin a;
        AdminPK aPK= new AdminPK(addNumber, addNumber, addNumber, (max.add(addNumber)));
        a= new Admin(aPK); 
        adminFacade.create(a);
        }catch (Exception e){return false;}      
              
        return true;
        
    }
    
    public boolean checkUser (String name)
    {
        PersonalInfo personalInfo;
         Query q1= em.createQuery("SELECT pi FROM PersonalInfo pi WHERE pi.user = :A");
         q1.setParameter("A", name);
         
         try{
            personalInfo=(PersonalInfo) q1.getSingleResult();
         } catch(NoResultException e) {
           return false;
        }
         return true;
    }
}
